//
//  MainViewController.swift
//  WeiBo
//
//  Created by tusm on 2017/6/15.
//  Copyright © 2017年 GM. All rights reserved.
//

import UIKit

class MainViewController: UITabBarController {
    //MARK: 按钮点击方法
    func clickComposedButton(){
        
        print("点我了")
    }

    //MARK: 生命周期
    override func viewDidLoad() {
        super.viewDidLoad()

        addChildViewControllers()
        
        setupComposedButton()
    }
    
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        
        tabBar.bringSubview(toFront: composedButton)
    }
    
    //MARK: 懒加载控件
    lazy var composedButton:UIButton = UIButton(imageName: "tabbar_compose_icon_add", bgImageName: "tabbar_compose_button")
        

}

//MARK:视图界面设置
extension MainViewController{
    
    //MARK:添加按钮方法
    func setupComposedButton() {
        
        tabBar.addSubview(composedButton)
        
        let count = childViewControllers.count
        
        let w = tabBar.bounds.size.width / CGFloat(count)
        
        composedButton.frame = CGRect(x: 2 * w, y: 0, width: w, height: tabBar.bounds.size.height)
        
        ///按钮点击事件
        composedButton.addTarget(self, action:#selector(MainViewController.clickComposedButton), for: .touchUpInside)
    }
    
    //MARK:添加子视图
     func addChildViewControllers() {
        
        tabBar.tintColor = UIColor.orange
        
        addChildViewController(vc: HomeViewController(), title: "首页", name: "tabbar_home")
        addChildViewController(vc: MessageViewController(), title: "消息", name: "tabbar_message_center")
        
        addChildViewController(UIViewController())
        
        addChildViewController(vc: DiscoverViewController(), title: "发现", name: "tabbar_discover")
        addChildViewController(vc: ProfileViewController(), title: "我", name: "tabbar_profile")
        
        
    }
    
    
    //MARK:子视图
    private func addChildViewController(vc: UIViewController, title:String, name:String) {
        //set title
        vc.title = title
        
        vc.tabBarItem.image = UIImage(named: name)
        
        let nav = UINavigationController(rootViewController: vc)
        
        
        addChildViewController(nav)
        
    }
}


